﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System.IO;
using System.Diagnostics;
using Business.Cdc;
using DAO.Cdc;

namespace Cdc
{
    /// <summary>
    /// Interaction logic for Generation.xaml
    /// </summary>
    public partial class Generation : UserControl
    {
        public Generation()
        {
            InitializeComponent();
            this.ViewDg.OnlyNotReported = true;
        }



        private void refresh(object sender, RoutedEventArgs e)
        {
            this.ViewDg.refresh();

        }

        private void MoveUp(object sender, System.EventArgs e)
        {
            DevExpress.Wpf.Grid.GridViewBase view = this.ViewDg.gridControl1.View;
            view.Focus();
            int index = view.FocusedRowHandle;
            if (index > 0)
            {
                FileDb val1 = (FileDb)this.ViewDg.gridControl1.GetRow(index);
                this.ViewDg.ListToShow.RemoveAt(index);
                this.ViewDg.ListToShow.Insert(index - 1, val1);
                view.FocusedRowHandle = index - 1;
            }
        }

        private void MoveDown(object sender, System.EventArgs e)
        {
            DevExpress.Wpf.Grid.GridViewBase view = this.ViewDg.gridControl1.View;
            view.Focus();
            int index = view.FocusedRowHandle;
            if (index < this.ViewDg.ListToShow.Count-1)
            {
                FileDb val1 = (FileDb)this.ViewDg.gridControl1.GetRow(index);
                this.ViewDg.ListToShow.RemoveAt(index);
                this.ViewDg.ListToShow.Insert(index + 1, val1);
                view.FocusedRowHandle = index + 1;
            }
        }

        private void Delete(object sender, System.EventArgs e)
        {
            DevExpress.Wpf.Grid.GridViewBase view = this.ViewDg.gridControl1.View;
            view.Focus();
            int index = view.FocusedRowHandle;
            this.ViewDg.ListToShow.RemoveAt(index);
        }

        private void Add(object sender, System.EventArgs e)
        {
        }

        private void Generate(object sender, System.EventArgs e)
        {
            try
            {
                String tempDir = Environment.GetEnvironmentVariable("TEMP");
                PdfDocument finalPdf = CdcBusiness.PdfDocumentGenerator.generate1stPage(SubtitleTb.Text, MoreInfoTb.Text);
                MemoryStream ms = null;

                //PdfDocument firstPage = CdcBusiness.PdfDocumentGenerator.generate1stPage(SubtitleTb.Text);
                //ms = new MemoryStream();
                //firstPage.Save(ms, false);
                //PdfReader.Open(ms, PdfDocumentOpenMode.ReadOnly);
                //finalPdf.AddPage(CdcBusiness.PdfDocumentGenerator.getFileToImport(ms).Pages[0]);
                PdfDocument pdf;
                foreach (FileDb fileDb in this.ViewDg.ListToShow)
                {
                    ms = new MemoryStream(CdcFilesDAO.getFile(fileDb.Id));
                    pdf = CdcBusiness.PdfDocumentGenerator.getFileToImport(ms);
                    foreach (PdfPage page in pdf.Pages)
                    {
                        finalPdf.AddPage(page);
                    }
                }

                String fileName = tempDir + @"\report_" + DateTime.Now.ToString("ddmmyyyy_HHMMss") + ".pdf";
                finalPdf.Save(fileName);
                finalPdf.Close();
                Process.Start(fileName);

                MessageBoxResult dlgResult =
                    MessageBox.Show("Do you want to send this file by mail ?", "MAIL ?", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (dlgResult == MessageBoxResult.Yes)
                {
                    Window w = new Window();
                    w.Width = 300.0;
                    w.Height = 300.0;
                    w.Content = new Mail(fileName);
                    w.ShowDialog();
                }
            }
            catch (IOException ex)
            {
                MessageBox.Show("An exception occured during the pdf generation \n"+ex.StackTrace, "Error !!!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
